package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.CategoryService;
import cn.bytecollege.admin.service.CuisineService;
import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Category;
import cn.bytecollege.common.entity.Cuisine;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CuisineAddController", value = "/admin/cuisineAdd")
public class CuisineAddController extends HttpServlet {
    private CuisineService cuisineService = null;
    private CategoryService categoryService = null;
    public CuisineAddController(){
        cuisineService = new CuisineService();
        categoryService = new CategoryService();

    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //封装分类的数据，显示在下拉框中
        List<Category> list = categoryService.find();
        request.setAttribute("list",list);
       request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineAdd.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收数据
        String cuisineName = request.getParameter("cuisineName");
        String cuisinePrice = request.getParameter("price");
        String categoryId = request.getParameter("category");
        String cuisinePhoto = request.getParameter("photo");

        //2.封装数据()
        Cuisine cuisine = new Cuisine();
        cuisine.setCuisineName(cuisineName);
        cuisine.setCuisinePrice(Double.valueOf(cuisinePrice));
        cuisine.setCategoryId(Integer.valueOf(categoryId));
        cuisine.setCuisinePhoto(cuisinePhoto);

        //3.调用service方法
        cuisineService.save(cuisine);

        //4.跳转页面
        response.sendRedirect(request.getContextPath()+"/admin/cuisineList");
    }
}
